package cn.zyl.quartz.Job;

import cn.zyl.quartz.bean.JobParameter;
import org.quartz.*;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/InitJob")
public class InitJob extends HttpServlet{

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String name = req.getParameter("name");

        /**
         *  Job 1
         */
        JobParameter parameter1 = new JobParameter();
        parameter1.setJobName("job1");
        parameter1.setJobGroup("jobGroup");
        parameter1.setDescription("这是第一个Job,输出Hello {Name}");
        parameter1.setCronExpression("0/5 * * * * ?");

        /**
         * Job 2
         */

        JobParameter parameter2 = new JobParameter();
        parameter2.setJobName("job2");
        parameter2.setJobGroup("jobGroup");
        parameter2.setDescription("这是第二个Job,输出Hi World");
        parameter2.setCronExpression("0/2 * * * * ?");

        AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        try{

            //构建Job1信息
            Class jobClass = Class.forName("cn.zyl.quartz.Job.Job1");
            JobDetail job1 = JobBuilder.newJob(jobClass )
                    .withIdentity( parameter1.getJobName(), parameter1.getJobGroup())
                    .withDescription( parameter1.getDescription() )
                    .build();

            job1.getJobDataMap().put("name","zhaoyunlong");

            //构建Job2信息
            JobDetail job2 = JobBuilder.newJob(Job2.class)
                    .withIdentity( parameter2.getJobName(), parameter2.getJobGroup()).build();

            // Job1 表达式调度构建器
            // withMisfireHandlingInstructionFireAndProceed : 以当前时间为触发频率立刻触发一次执行  然后按照Cron频率依次执行
                CronScheduleBuilder scheduleBuilder1 = CronScheduleBuilder.cronSchedule(parameter1.getCronExpression());
            CronTrigger trigger1 = TriggerBuilder.newTrigger().withIdentity( parameter1.getJobName(), parameter1.getJobGroup())
                    .withSchedule(scheduleBuilder1.withMisfireHandlingInstructionDoNothing()).build();


            //Job2 表达式调度构建器材
            CronScheduleBuilder scheduleBuilder2 = CronScheduleBuilder.cronSchedule( parameter2.getCronExpression());
            CronTrigger trigger2 = TriggerBuilder.newTrigger().withIdentity( parameter2.getJobName(), parameter2.getJobGroup())
                    .withSchedule( scheduleBuilder2.withMisfireHandlingInstructionDoNothing() ).build();

            //调度器
            Scheduler scheduler = (Scheduler) context.getBean("scheduler");

            //如果数据库中包含该job，总不创建次job，而是继续它，或者启动它

            scheduler.scheduleJob(job1, trigger1);
            scheduler.scheduleJob(job2, trigger2);

            scheduler.start();

        } catch ( SchedulerException e ){
            e.printStackTrace();
        } catch ( ClassNotFoundException e ){
            e.printStackTrace();
        }
    }
}
